package cn.itcast.p2p20.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import cn.itcast.p2p20.utils.Util;

/**
 * Created by Administrator on 2016/7/26.
 */
public class ProgressRing extends View {

    private Paint mGrayPaint;
    private float mGrayCirlceWidth;
    private Paint mRedArc;
    private Paint mPaintText;

    public ProgressRing(Context context) {
        this(context, null);
    }

    public ProgressRing(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressRing(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mGrayPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mGrayPaint.setColor(Color.GRAY);
        //镂空
        mGrayPaint.setStyle(Paint.Style.STROKE);
        //设置画笔宽度
        mGrayCirlceWidth = Util.dipToPx(context, 8);
        mGrayPaint.setStrokeWidth(mGrayCirlceWidth);

        mRedArc = new Paint(Paint.ANTI_ALIAS_FLAG);
        mRedArc.setColor(Color.RED);
        mRedArc.setStyle(Paint.Style.STROKE);
        mRedArc.setStrokeWidth(mGrayCirlceWidth);

        mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintText.setColor(Color.BLUE);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        drawGrayCirlce(canvas);

        drawRedProgress(canvas);

        drawText(canvas);

    }

    private void drawText(Canvas canvas) {

        String text = mRrogress + "%";// 73.33%
        //public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint)
        //同画笔获取字体的宽度和高度
        // public void getTextBounds(String text, int start, int end, Rect bounds)
        Rect bounds = new Rect();
        mPaintText.getTextBounds(text, 0, text.length(), bounds);
        int fontWidth = bounds.width();
        int fontHeight = bounds.height();
        float x = mCenter.x - fontWidth * 0.5f;
        float y = mCenter.y + fontHeight * 0.5f;

        canvas.drawText(text, x, y, mPaintText);
    }

    private float mRrogress = 73.33f;

    private RectF oval = new RectF();

    private void drawRedProgress(Canvas canvas) {

        //sweepAngle圆弧的大小
        //useCenter true 扇形
        //public void drawArc(@NonNull RectF oval, float startAngle, float sweepAngle, boolean useCenter,@NonNull Paint paint)
        canvas.drawArc(oval, -90, 360 * mRrogress / 100, false, mRedArc);
    }

    private void drawGrayCirlce(Canvas canvas) {

        //public void drawCircle(float cx, float cy, float radius, @NonNull Paint paint)
        canvas.drawCircle(mCenter.x, mCenter.y, mRadius, mGrayPaint);
    }

    private PointF mCenter = new PointF();
    private float mRadius;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        //圆心
        mCenter.set(measuredWidth * 0.5f, measuredHeight * 0.5f);
        //半径 ，需要减去画笔宽度的一半
        mRadius = Math.min(measuredWidth, measuredHeight) * 0.5f - mGrayCirlceWidth * 0.5f;
        //沿画笔的轨迹的外接矩形
        oval.set(mCenter.x - mRadius, mCenter.y - mRadius, mCenter.x + mRadius, mCenter.y + mRadius);
    }

    public void setProgress(double raiseProgress) {

        mRrogress = (float) raiseProgress;
        postInvalidate();
    }
}
